package knh;

//import android.*;
import android.app.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.widget.*;
import knh.Item;
import com.example.crystall_ball.*;

public class DB_Items{
	private DBHelper mHelper;
	private boolean load = false;
	private int item_limit = 100;
	private String DB_Name = new String("items");
	
	public int getItem_Limit(){
		return this.item_limit;
	}
	
	public DB_Items(Object obj){
		mHelper = new DBHelper((Context)obj);
		SQLiteDatabase db = mHelper.getWritableDatabase();
		mHelper.onUpgrade(db, 0, 0);
	}

	private class DBHelper extends SQLiteOpenHelper{
		public DBHelper(Context context){
			super(context,"Item.db",null,1);
		}
		public void onCreate(SQLiteDatabase db){
			db.execSQL("CREATE TABLE "+DB_Name+" ( _id VARCHAR PRIMARY KEY," +
					"name VARCHAR,icon INT);");
		}
		public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
			db.execSQL("DROP TABLE IF EXISTS "+DB_Name);
			onCreate(db);
		}
	}
	
	public Item LoadData(String id){
		String name = new String("");
		int icon;
		SQLiteDatabase db = mHelper.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM "+ DB_Name+" WHERE _id = '"+id+"'",null);
		
		if(cursor.moveToNext()){
			name = cursor.getString(1);
			icon = Integer.parseInt(cursor.getString(2));
			db.close();
		}else{
			db.close();
			return new Item("-1","");
		}
		return new Item(id,name,icon);
	}
	
	public Item[] LoadAllData(){
		Item items[] = new Item[item_limit];
		String id = new String("");
		String name = new String("");
		int icon,cnt=0;
		SQLiteDatabase db = mHelper.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM "+DB_Name,null);
		while(cursor.moveToNext()){
			id = cursor.getString(0);
			name = cursor.getString(1);
			icon = Integer.parseInt(cursor.getString(2));
			items[cnt++] = new Item(id,name,icon);
		}
		return items;
	}
	
	public boolean SaveData(Item item){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		try{
			db.execSQL("INSERT INTO "+ DB_Name + " VALUES('"+item.getId()+"','"
					+item.getName()+"',"+item.getIcon()+");");
		}catch(SQLiteConstraintException e){
			return false;
		}finally{
			db.close();
		}
		return true;
	}
	
	public void SaveData(Item[] item){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		for(Item i:item){
			db.execSQL("INSERT INTO "+DB_Name+" VALUES('"+i.getId()+"','"
					+i.getName()+"',"+i.getIcon()+");");
		}
		db.close();
	}
	
	public void DeleteData(String id){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+DB_Name+" WHERE _id = '"+id+"';");
		db.close();
	}
	
	public void DeleteAllData(){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		db.execSQL("DELETE * FROM "+DB_Name+";");
		db.close();
	}
	
	public void UpdateData(Item item){
		DeleteData(item.getId());
		SaveData(item);
	}
}